﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace Easy.Common.Transaction
{
    class TransactionCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
            {
                var methodReturn = getNext().Invoke(input, getNext);

                if (methodReturn.Exception == null)
                {
                    transactionScope.Complete();
                }

                return methodReturn;
            }
        }

        public int Order { get; set; }
    }
}
